



Window_Base.prototype.processEscapeCharacter = function (code, textState) {
    switch (code) {
        case "E":
            this.processNewLineSp(textState);
            break;
        case "C":
            this.processColorChange(this.obtainEscapeParam(textState));
            break;
        case "I":
            this.processDrawIcon(this.obtainEscapeParam(textState), textState);
            break;
        case "PX":
            textState.x = this.obtainEscapeParam(textState);
            break;
        case "PY":
            textState.y = this.obtainEscapeParam(textState);
            break;
        case "FS":
            this.contents.fontSize = this.obtainEscapeParam(textState);
            break;
        case "{":
            this.makeFontBigger();
            break;
        case "}":
            this.makeFontSmaller();
            break;
    }
}

/* 处理新行 */
Window_Base.prototype.processNewLineSp = function (textState) {
    textState.x = textState.startX;
    textState.y += textState.height + 12;
    textState.height = this.calcTextHeight(textState);
}

class Scene_Chat extends Scene_Base {
    initialize() {
        super.initialize();
        this._realScale = Graphics._realScale;
        NetMap.needPlacePlayer = true;
    }

    create() {
        super.create();
        this.createWindowLayer();
        this.createWindow();
        this.createInput();
        this.createButton();
    }

    createWindow() {
        const rect = this.chatWindowRect();
        this._chatWindow = new Window_Chat(rect);
        this.addWindow(this._chatWindow);
    }

    chatWindowRect() {
        const wx = 0;
        const ww = Graphics.boxWidth;
        const wh = 1000;
        const wy = (Graphics.boxHeight - wh) / 2;
        return new Rectangle(wx, wy, ww, wh);
    }

    createInput() {
        if (!this._input) {
            this._input = NetManager.createInput();
        }
        this.updateInput();
        document.body.appendChild(this._input);
    }

    createButton() {
        this.createChatButton();
        this.createBackButton();
    }

    createChatButton() {
        const x = 5;
        const y = 1160;
        const bitmap = this.chatButtonBitmap();
        const chatButton = new Sprite_NetButton(x, y, bitmap, this.chat.bind(this));
        this._chatButton = chatButton;
        this.addChild(chatButton);
    }

    chatButtonBitmap() {
        const w = this.width;
        const h = 100;
        const c = "#DBDBDB";
        const c2 = "#BBFFFF";
        const bitmap = new Bitmap(w, h);
        bitmap.fillRoundRect(0, 0, w, h / 2, c);
        bitmap.fillRoundRect(0, h / 2, w, h / 2, c2);
        bitmap.fontSize = 30;
        bitmap.drawText("chat", 0, 5, w, h / 2, "center");
        return bitmap;
    }

    createBackButton() {
        const x = Graphics.boxWidth - 121;
        const y = 25;
        const button = new Sprite_Button("cancel");
        button.setClickHandler(this.popScene.bind(this));
        this._backButton = button;
        this.addChild(button);
        button.move(x, y);
    }

    chat() {
        const text = this._input.value;
        if (text !== "") {
            NetMap.sendMessage(text);
        }
    }

    popScene() {
        super.popScene();
        if (this._input) this._input.remove();
    }

    update() {
        super.update();
        if (this._realScale != Graphics._realScale) {
            this.updateInput();
        }
    }

    updateInput() {
        if (!this._input) return;
        const x = this._chatWindow.pageX();// + 170 * Graphics._realScale;
        const y = 5;//+ (this._chatWindow.height + this._chatWindow.y) * Graphics._realScale;
        const w = this._chatWindow.width * Graphics._realScale;
        const h = 35 * Graphics._realScale;
        this._input.style.left = `${x}px`;
        this._input.style.bottom = `${y}px`;
        this._input.style.width = `${w}px`;
        this._input.style.height = `${h}px`;
        this._realScale = Graphics._realScale;
    }
}

class Window_Chat extends Window_Scrollable {
    initialize(rect) {
        super.initialize(rect);
        this._message = [];
        this._overallHeight = 0;
        // this.test();
        this.contents.fontSize = 30;
        this.refresh();
    }

    refresh() {
        this._message = NetMap.message.all;
        this.paint();
        this.smoothScrollTo(0, this.maxScrollY());
    }

    paint() {
        this.contents.clear();
        this._overallHeight = 0;
        const x = 10;
        let y = 30 - this._scrollY;
        this.drawMessage(x, y);
    }

    drawMessage(x, y) {
        const text = this.makeText();
        this.drawNetText(text, x, y, 680);
    }

    makeText() {
        const text = [];
        const data = NetMap.message.data;
        for (const index of this._message) {
            const name = data[index][0]
            const message = data[index][1];
            const tempText = `\\C[20][世界]\\C[23] ${name}\\C[6]: ${message}`;
            text.push(tempText);
        }
        return text;
    }

    overallHeight() {
        return this._overallHeight + 50;
    }

    test() {
        for (let i = 0; i < 20; i++) {
            const text = "对子表达式的引用，是指引用前面子表达式所匹配的文本，而不是子表达式的匹配模式。如果要引用前面子表达式的匹配模式，则必须使用下面方式，只有这样才能够达到匹配目的";
            const name = "sun";
            NetMap.message.data.push([name, text]);
            NetMap.message.all.push(i);
        }
    }
}
